<?php
/**
 * 媒资管理控制器
 */

namespace app\admin\controller;


use app\admin\service\MediaAssetService;
use app\common\Common;
use app\common\Error;
use app\Request;

class MediaAssets extends Base
{

    public $service;

    public function __construct(MediaAssetService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    //同步短剧
    public function syncShortPlay(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->shortPlayThirdPartySync($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //媒资上传（可批量）
    public function uploadVideos(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id']) || empty($params['ep_ids'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->videosThirdPartyUpload($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //同步绑定剧集
    public function syncVideos(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id']) || empty($params['ep_ids'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->videosThirdPartySync($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //送审
    public function audit(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->thirdPartyAudit($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //送审回调
    public function dyCallback(Request $request)
    {
        $params = $request->param();
        flog([
            'params' => $params,
        ],'douyin_callback');

        $result = $this->service->dyCallback($params);

        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit();
    }

    //回调前置配置
    public function configureCallback(Request $request)
    {
        $params = $request->param();
        $result = $this->service->thirdPartyConfig($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }
        return Common::success();
    }

    //上线
    public function online(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->thirdPartyOnline($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //短剧授权
    public function authorize(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->thirdPartyAuthorize($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //微信短剧授权
    public function wechatAuthorize(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['video_id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->thirdPartyAuthorize($params, 1);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //微信短剧授权查询
    public function wechatAuthorizeQuery(Request $request)
    {
        if (!$request->isGet()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['video_id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->wechatAuthorizeQuery($params);

        return Common::success($result);
    }

    //页面绑定
    public function pageBind(Request $request)
    {
        if (!$request->isPost()){
            return Common::fail('错误的请求', 404);
        }

        $params = $request->param();
        if (empty($params['id']) || empty($params['ep_ids'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->batchPageBinding($params);
        if ($result) {
            count($result) > 1 ? [$msg, $code] = $result : $msg = $result[0];
            $code = $code ?? Error::FAIL_CODE;
            return Common::fail($msg, $code);
        }

        return Common::success();
    }

    //抖音短剧详情查询接口
    public function dyMedioQuery(Request $request)
    {
        $params = $request->param();
        if (empty($params['id'])) {
            return Common::fail('参数错误', 404);
        }

        $result = $this->service->dyAlbumDetailQuery($params);
        if ($result['msg']) {
            return Common::fail($result['msg']);
        }

        return Common::success($result['data']);
    }
}